package com.myph.phctrl_t;

import com.example.phctrl_t.R;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;

public class MCUSleepActivity extends Activity implements OnClickListener{

    RelativeLayout layout_on, layout_sleep_on;
    Button btn_submit;
    Switch swi_on, swi_sleep_mode;
    String ip, port;
    String on_or_off = "8", sleep_on_or_off = "8";
    StringBuilder response = new StringBuilder();

    private long prevClickTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);

        ip = Util.getSharedPref(this, "ip");
        port = Util.getSharedPref(this, "port");

        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
        
      

        swi_sleep_mode = (Switch) findViewById(R.id.swi_sleep_mode);
        swi_sleep_mode
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                            sleep_on_or_off = "1";
                            Toast.makeText(getApplicationContext(),
                                    "MCU sleep ON", Toast.LENGTH_SHORT).show();
                        } else {
                            sleep_on_or_off = "0";
                            Toast.makeText(getApplicationContext(),
                                    "MCU sleep OFF", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

        layout_on = (RelativeLayout) findViewById(R.id.layout_on);
        layout_sleep_on = (RelativeLayout) findViewById(R.id.layout_sleep_on);
        layout_on.setVisibility(View.GONE);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:

                String request = "$sleep," + sleep_on_or_off + "#";
                ClientSocketNew csn = new ClientSocketNew(ip, port, handler);
                csn.setNewRequest(request, response);
                csn.start();
                break;
            case 1002:
                Toast.makeText(getApplicationContext(), "fail to connect",
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            case 1003:
                if (response != null) {
                    Log.d("response:", response.toString());
                    String resString = response.toString();
                    if (resString.equals("1")) {
                        Toast.makeText(getApplicationContext(), "成功",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "失败",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "异常!",
                            Toast.LENGTH_SHORT).show();
                }
                finish();
                break;
            case 1004:
                Toast.makeText(getApplicationContext(), "操作失败",
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                Toast.makeText(getApplicationContext(), "handler error!",
                        Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
    
	@Override
	public void onClick(View v) {
		long pressGap = System.currentTimeMillis() - prevClickTime ;
		if(pressGap < 1000){
			return;
		}else{
			prevClickTime = System.currentTimeMillis();
		}
		switch (v.getId()) {
		case R.id.btn_submit:
			Message msg = new Message();
            msg.what = 1;
            handler.sendMessage(msg);
			break;

		default:
			break;
		}
	}
}
